<script setup>
import {onMounted, ref} from "vue";
import axios from "axios";
import {ElMessage} from "element-plus";
import qs from "qs";
onMounted(()=>{
  loadBlacklist();
})
//控制添加黑名单弹窗是否显示
const dialogVisible = ref(false);
const auditDialogVisible = ref(false);
const rejectDialogVisible = ref(false);
//定义变量控制dialog的标题
const dialogTitle = ref();
//点击"添加黑名单"按钮,让弹窗出现且改标题
const beforeAddBlackList = ()=>{
  dialogTitle.value = "添加黑名单";
  dialogVisible.value = true;
  auditDate.value = {};
}
const auditDate = ref({
  parkingId:'',
  userLicense:'',
  reason:'',
  audit:''
})
//添加黑名单
const addBlack = ()=>{
  let data = qs.stringify(auditDate.value)
  axios.post(BASE_URL+"/v1/black/insert",data).then((response)=>{
    if(response.data.code==2000){
      ElMessage.success("添加成功");
      //关闭弹窗
      dialogVisible.value = false;
      //清空数据
      auditDate.value = {};
      //重新加载数据
      loadBlacklist();
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}
//定义一个状态转换的方法
const blackNameStatus = (row,column,cellValue,index)=>{
  if(cellValue == 10){
    cellValue = '未审核';
  }else if(cellValue == 20){
    cellValue = '已审核';
  }else if(cellValue == 20){
    cellValue = '已审核';
  }else if (cellValue == 30){
    cellValue = '驳回';
  }
  return cellValue;
}
//点击”审批“按钮,让弹窗出现且改标题
const beforeAudit = (id)=>{
  dialogTitle.value = "审批 详情";
  auditDialogVisible.value = true;
  axios.get(BASE_URL+"/v1/black/select?id="+id).then((response)=>{
    if(response.data.code==2000){
      auditDate.value = response.data.data[0];
      console.log(auditDate.value);
      if (auditDate.value.audit == 10){
        dialogTitle.value="审批 详情"
      }else {
        dialogTitle.value="驳回 原因"
      }
    }else {
      ElMessage.error(response.data.msg);
    }
  })
}
//处理弹窗关闭的方法
const handleClose = ()=>{
  if(confirm("确定要关闭弹窗吗?")){
    //关闭弹窗且清空已输入的数据
    dialogVisible.value = false;
    auditDialogVisible.value = false;
    rejectDialogVisible.value = false;
    //清空数据
    rejectReason.value = '';
    auditDate.value = {};
  }
}
//定义方法加载所有的黑名单数据
const loadBlacklist = ()=>{
  searchBlackList.value.audit = type.value;
  let data = qs.stringify(searchBlackList.value)
  axios.get(BASE_URL+"/v1/black/select?"+data).then((response)=>{
    if(response.data.code==2000){
      blackListArr.value = response.data.data;
      console.log(response.data.data);
    }else{
      ElMessage.error(response.data.msg);
    }
  })
 }

//定义重置按钮的方法
const resetSearch = ()=>{
  //清空搜索数据
  searchBlackList.value = {}
  //重新加载所有的用户数据
  loadBlacklist();
}
//定义对象用来保存搜索卡片的黑名单搜索条件
const searchBlackList = ref({parkingId:'',userLicense:'',  audit:''});
//定义数组保存黑名单数据列表
let blackListArr = ref([])
//定义变量保存上方审批状态单选组的值,且设置默认选中10(待审核)按钮
const type = ref("10");
//审核通过
const auditPass = ()=>{
  auditDate.value.auditStatus = "10";
  let data = qs.stringify(auditDate.value)
  axios.post(BASE_URL+"/v1/black/update",data).then((response)=>{
    if(response.data.code==2000){
      ElMessage.success("审核通过");
      //关闭弹窗
      auditDialogVisible.value = false;
      //重新加载数据
      loadBlacklist();
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}
//驳回
const rejectReason = ref('');
const auditReject = ()=>{
  if(!rejectReason.value || rejectReason.value.trim()==''){
    ElMessage.error("驳回原因不能为空！");
    return;
  }
  rejectDialogVisible.value = false;
  auditDialogVisible.value = false;
  auditDate.value.reason = rejectReason.value;
  auditDate.value.audit = 30;
  let data = qs.stringify(auditDate.value)
  axios.post(BASE_URL+"/v1/black/update",data).then((response)=>{
    if (response.data.code==2000){
      ElMessage.success("驳回成功")
      auditDate.value = {};
      rejectReason.value = '';
      loadBlacklist();
    }else {
      ElMessage.error(response.data.msg)
    }
  })
}
</script>

<template>
<!-- 顶部条 -->
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">黑名单列表</span>
    <el-button type="primary"style="float:right;margin-top:10px;"
               @click="beforeAddBlackList">添加黑名单</el-button>
  </div>
  <!-- 搜索卡片 -->
  <el-card style="margin:20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="停车场编号">
        <el-input placeholder="请输入停车场编号" style="width:220px;" v-model="searchBlackList.parkingId"></el-input>
      </el-form-item>
      <el-form-item label="车牌号">
        <el-input placeholder="请输入车牌号" style="width:220px;" v-model="searchBlackList.userLicense"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="loadBlacklist">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>
<!-- 添加黑名单弹窗 -->
  <el-dialog :title="dialogTitle"
             v-model="dialogVisible"
             style="width: 1000px;padding:40px;"
             :before-close="handleClose"
  >
    <el-form label-width="80px" label-position="top">
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="停车编号">
            <el-input placeholder="请输入停车场编号" v-model="auditDate.parkingId"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车牌号">
            <el-input placeholder="请输入车牌号" v-model="auditDate.userLicense"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="原因">
            <el-input type="textarea" resize="none" placeholder="请描述违规原因" v-model="auditDate.reason"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="addBlack">确定</el-button>
    </template>
  </el-dialog>
<!-- 黑名单数据列表 -->
  <el-card style="margin: 20px;">
    <!--  审批状态项  -->
    <el-radio-group style="margin-bottom: 15px;" v-model="type" @change="loadBlacklist">
      <el-radio-button value="10" size="large">未审核</el-radio-button>
      <el-radio-button value="20" size="large">已审核</el-radio-button>
      <el-radio-button value="30" size="large">驳回</el-radio-button>
    </el-radio-group>
    <!--  数据列表  -->
    <el-table style="width: 100%" :data="blackListArr">
      <el-table-column align="center" label="序号" type="index" width="80" />
      <el-table-column align="center" label="停车场编号" prop="parkingId" />
      <el-table-column align="center" label="车牌号" prop="userLicense" />
      <el-table-column align="center" label="原因" prop="reason" />
      <el-table-column align="center" label="添加时间" prop="addtime" />
      <el-table-column align="center" label="申请状态" prop="audit" :formatter="blackNameStatus"/>
      <el-table-column align="center" label="申请时间" prop="auditTime" />
      <el-table-column align="center" label="创建时间" prop="createTime" />
      <el-table-column align="center" label="操作"  v-if="type==10||type==30">
        <template #default="scope">
          <!-- 审批10：在未审核页签下显示，需要当前登录用户进行审批 -->
          <el-button type="primary" link v-if="type==10" @click="beforeAudit(scope.row.id)">审批</el-button>
          <!-- 查看30：在驳回页签下显示，可以查看已驳回申请的驳回原因 -->
          <el-button type="primary" link v-if="type==30" @click="beforeAudit(scope.row.id)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>


  <!-- 审批/查看弹窗 -->
  <el-dialog :title="dialogTitle" v-model="auditDialogVisible">
    <el-descriptions direction="horizontal" :column="2" border>
      <el-descriptions-item label="停车场编号">{{auditDate.parkingId}}</el-descriptions-item>
      <el-descriptions-item label="用户车牌号">{{auditDate.userLicense}}</el-descriptions-item>
      <el-descriptions-item label="申请原因">{{auditDate.reason}}</el-descriptions-item>
    </el-descriptions>
    <template #footer  v-if="auditDate.audit==10">
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" plain @click="rejectDialogVisible=true">驳回</el-button>
      <el-button type="primary" @click="auditPass">通过</el-button>
    </template>
  </el-dialog>


  <!-- 驳回原因弹窗 -->
  <el-dialog title="驳回 查看" v-model="rejectDialogVisible" style="margin-top: 37vh;"
             :before-close="handleClose">
    <el-descriptions direction="horizontal" border>
      <el-descriptions-item label="驳回原因">
        <el-input placeholder="请输入驳回原因" v-model="rejectReason"></el-input>
      </el-descriptions-item>
    </el-descriptions>
    <template #footer>
      <el-button type="primary" plain @click="handleClose">取消</el-button>
      <el-button type="primary" @click="auditReject">确定</el-button>
    </template>
  </el-dialog>
</template>

<style scoped>

</style>